package com.snap.vseries.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.snap.vseries.MoleApplication;

import java.util.ArrayList;
import java.util.List;

public class SpUtils {

    public static final String CONTENT_LIST = "content_list";
    public static final String DEVICE_LIST = "device_list";
    private final static String SP_SET_CONFIG = "config";
    private static SpUtils instance;
    private SharedPreferences sp;

    private SpUtils() {
        sp = MoleApplication.get().getSharedPreferences(SP_SET_CONFIG, Context.MODE_PRIVATE);
    }

    public static SpUtils get() {
        if (instance == null) {
            instance = new SpUtils();
        }
        return instance;
    }

    public boolean putInt(String key, int value) {
        return sp.edit().putInt(key, value).commit();
    }

    public int getInt(String key, int defValue) {
        return sp.getInt(key, defValue);
    }

    public boolean putBoolean(String key, boolean value) {
        return sp.edit().putBoolean(key, value).commit();
    }

    public boolean getBoolean(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    public boolean putFloat(String key, float value) {
        return sp.edit().putFloat(key, value).commit();
    }

    public float getFloat(String key, float defValue) {
        return sp.getFloat(key, defValue);
    }

    public boolean putString(String key, String value) {
        return sp.edit().putString(key, value).commit();
    }

    public String getString(String key, String defValue) {
        return sp.getString(key, defValue);
    }

    public float getFloat(String key) {
        return getFloat(key, 0f);
    }

    public boolean clear(String key) {
        return sp.edit().remove(key).commit();
    }

    public boolean clearAll() {
        return sp.edit().clear().commit();
    }

    /**
     * 保存List
     *
     * @param tag
     * @param dataList
     */
    public <T> void setDataList(String tag, List<T> dataList) {
        try {
            SharedPreferences.Editor editor = sp.edit();
            if (null == dataList) {
                return;
            }
            String strJson = JSON.toJSONString(dataList);
            editor.putString(tag, strJson);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取List
     *
     * @param tag
     * @param resultType
     * @param <T>
     * @return
     */
    public <T> List<T> getDataList(String tag, Class<T> resultType) {
        List<T> dataList = new ArrayList<T>();
        String strJson = sp.getString(tag, null);
        if (null == strJson) {
            return dataList;
        }
        dataList = JSONObject.parseArray(strJson, resultType);
        return dataList;
    }

    public <T> void putObject(String tag, T data) {
        try {
            SharedPreferences.Editor editor = sp.edit();
            if (null == data) {
                return;
            }
            String strJson = JSON.toJSONString(data);
            editor.putString(tag, strJson);
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public <T> T getObject(String tag, Class<T> resultType) {
        T result;
        String strJson = sp.getString(tag, null);
        if (null == strJson) {
            return null;
        }
        result = JSONObject.parseObject(strJson, resultType);
        return result;
    }

}
